package com.example.restservice;

import java.util.concurrent.atomic.AtomicLong;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 控制器
 */
@RestController
public class GreetingController {

	/**
	 * 可替换的值
	 */
	private static final String template = "Hello, %s!";

	/**
	 * 一个可以自动更新的long值
	 */
	private final AtomicLong counter = new AtomicLong();

	/**
	 * http://localhost:8080/greeting
	 * @param name : 值
	 * @return : 返回json格式的数据
	 */
	@GetMapping("/greeting")
	public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
		return new Greeting(counter.incrementAndGet(), String.format(template, name));
	}
}
